/* 
 * @Author: 21克的爱情
 * @Date: 2020-09-03 12:53:54
 * @Email: raohong07@163.com
 * @LastEditors: 21克的爱情
 * @LastEditTime: 2020-10-05 23:33:41
 * @Description: 
 */
import 'dart:isolate';
import 'dart:math';

import 'package:extended_image/extended_image.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:ki5k_video/api/Api.dart';
import 'package:ki5k_video/models/origin.dart';
import 'package:ki5k_video/models/tab_list.dart';
import 'package:ki5k_video/models/video_list.dart';
import 'package:ki5k_video/provide/application_provide.dart';
import 'package:ki5k_video/resources/AppOriginConfig.dart';
import 'package:ki5k_video/utils/isolate.dart';
import 'package:ki5k_video/utils/sqflite/SQliteHelp.dart';
import 'package:ki5k_video/utils/sqflite/SQliteMessage.dart';
import 'package:provider/provider.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:xml/xml.dart';

import 'search_page.dart';
import 'video/video_detail_page.dart';

class HomePage extends StatefulWidget {
  HomePage({Key key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
  ScrollController _scrollController = ScrollController();
  RefreshController _refreshController =  RefreshController(initialRefresh: true);

  TabController _tabController;
  List<VideoType> tabData = [];

  bool showTab = false;
  ApplicationStatus applicationStatus;

  @override
  void initState() { 
    super.initState();


    final data = ApplicationStatus.navigatorKey.currentContext;
    applicationStatus = Provider.of<ApplicationStatus>(context, listen: false);
    print(data);

    getTabData();
  }

  void getTabData() async {
    final value = await Api().getHomeVideo({
      'ac': '',
      't': '',
      'h': '240',
      'pg': 1
    },index: applicationStatus.currentIndex,);
    if( value != null && value.isNotEmpty ){
      final document = XmlDocument.parse(value);
      tabData = document.findAllElements('ty').toList().skip(4).toList().map((e){
        return VideoType.fromJson({
          'type_id' : e.attributes.last.value,
          'type_name': e.text
        });
      }).toList();
      
      // 设置第一个视频类型开始加载
      applicationStatus.selectVideoType = tabData[0].typeId;
      // 初始化tab选择
      _tabController = new TabController(length: tabData.length, vsync: this);

      if(!mounted) return;
      setState(() {});
    }
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _tabController?.dispose();
    _scrollController?.dispose();
  }

  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: NestedScrollView(
        scrollDirection: Axis.vertical,
        controller: _scrollController,
        physics: NeverScrollableScrollPhysics(),
        headerSliverBuilder: (BuildContext context, bool type) {
          return <Widget>[
            SliverAppBar(
              pinned: true,
              floating: true,
              title: Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: <Widget>[
                  SizedBox(
                    width: 30,
                    child: PopupMenuButton<String>(
                    offset: Offset(0, 0),
                    padding: EdgeInsets.all(0),
                    icon: Icon(
                      Icons.switch_video,
                      size: 28,
                    ),
                    onSelected: (value) {
                      print('$value');
                      setState(() {
                        int index = AppOriginConfig().originList.indexWhere((element){
                          return element.url == value;
                        });
                        applicationStatus.currentIndex = index;
                        getTabData();
                        _refreshController.requestRefresh();
                      });
                    },
                    itemBuilder: (context) {
                      return AppOriginConfig().originList.map((Origin item){
                        return PopupMenuItem<String>(
                          value: '${item.url}',
                          enabled: AppOriginConfig().originList[applicationStatus.currentIndex].url != item.url,
                          child: Text('${item.name}'),
                        );
                      }).toList();
                    },
                  ),
                  ),
                  Expanded(
                    child: InkWell(
                      onTap: () {
                        print('打开搜索界面');
                        Navigator.of(context).push(
                          MaterialPageRoute(builder: (_){
                            return SearchVideoPage( );
                          })
                        );
                      },
                      child: Container(
                        margin: EdgeInsets.only(left: 10),
                        padding: EdgeInsets.symmetric(
                          vertical: 6,
                          horizontal: 8,
                        ),
                        decoration: BoxDecoration(
                          gradient: LinearGradient(
                            colors: [
                              Colors.black12,
                              Colors.black12,
                              Colors.black12,
                            ],
                            begin: FractionalOffset(1, 0),
                            end: FractionalOffset(0, 1),
                          ),
                          borderRadius: BorderRadius.all(
                            Radius.circular(20),
                          ),
                        ),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Container(
                              padding: EdgeInsets.only(
                                left: 10
                              ),
                              child: Text(
                                '搜索视频',
                                style: TextStyle(
                                  fontSize: 15,
                                  color: Colors.white60
                                ),
                              ),
                            ),
                            Icon(
                              Icons.search,
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                ],
              ),
              bottom: new PreferredSize(
                preferredSize: Size.fromHeight( showTab ? 148 : 48.0),
                child: tabData.length == 0 ? Container() : Row(
                  children: <Widget>[
                    Expanded(
                      child: TabBar(
                        isScrollable: true,
                        controller: _tabController,
                        tabs: getTabItem,
                        onTap: (int index){
                          if (applicationStatus.selectVideoType == tabData[index].typeId) return;
                          print('选中${tabData[index].typeId}');
                          applicationStatus.selectVideoType = tabData[index].typeId;
                          // _refreshController.requestRefresh();
                          // _loadPageData(true, 1,);
                        },
                      ),
                    ),
                    InkWell(
                      onTap: () {
                        print('打开菜单');
                        // SQliteHelp.instance.cleanData(SQliteMessage.tablePlayData);
                        SQliteHelp.instance.deleteTable;
                        setState(() {
                          // showTab = !showTab;
                        });
                      },
                      child: Container(
                        padding: EdgeInsets.symmetric(vertical: 5, horizontal: 12),
                        child: Icon(
                          Icons.list,
                          color: Colors.white,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ];
        },
        body: _tabController != null ? TabBarView(
          controller: _tabController,
          children: tabData.map((item){
            return RefreshListView(_refreshController);
          }).toList()
        ) : Container()
      ),
    );
  }


  // 加载tab选项
  List<Widget> get getTabItem {
    List<Widget> list = [];
    tabData.forEach((item) {
      list.add(Tab(
        child: Text('${item?.typeName}'),
      ));
    });
    return list;
  }

}
class RefreshListView extends StatefulWidget {
  final RefreshController _refreshController;
  RefreshListView(this._refreshController, {Key key}) : super(key: key);

  @override
  _RefreshListViewState createState() => _RefreshListViewState();
}

class _RefreshListViewState extends State<RefreshListView> {
  List<VideoDetail> listDatas = [];
  SendPort blueSender;
  Isolate isolate;
  int pageIndex = 1;

  List<Widget> listVideo = [];

  @override
  void initState() { 
    super.initState();
    createIsolate();
  }

  @override
  Widget build(BuildContext context) {
    return SmartRefresher(
      // enablePullDown: true,
      enablePullUp: listDatas.length != 0,
      header: WaterDropHeader(),
      footer: CustomFooter(
        builder: (BuildContext context, LoadStatus mode) {
          Widget body;
          if (mode == LoadStatus.idle) {
            body = Text("pull up load");
          } else if (mode == LoadStatus.loading) {
            body = CupertinoActivityIndicator();
          } else if (mode == LoadStatus.failed) {
            body = Text("Load Failed!Click retry!");
          } else if (mode == LoadStatus.canLoading) {
            body = Text("Release to Load more");
          } else {
            body = Text("No more Data");
          }
          return Container(
            height: 55.0,
            child: Center(child: body),
          );
        },
      ),
      controller: widget._refreshController,
      onRefresh: () async {
      pageIndex = 1;
        // initPageData(true);
        isolatePageData(listDatas, pageIndex, true);
        // initPageData(true);
      },
      onLoading: () async {
        // 判断是否可以加载数据
        if(widget._refreshController.isLoading){
          pageIndex++;
          // initPageData(false);
          isolatePageData(listDatas, pageIndex, false);
        }
      },
      child: listDatas.length > 0 ? GridView.builder(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 3,
          mainAxisSpacing: 15,
          crossAxisSpacing: 6,
          childAspectRatio: 0.6,
        ),
        shrinkWrap: true,
        padding: EdgeInsets.symmetric(
          vertical: 5,
          horizontal: 5,
        ),
        physics: NeverScrollableScrollPhysics(),
        itemCount: listDatas.length,
        scrollDirection: Axis.vertical,
        itemBuilder: (BuildContext context, int index) {
          return InkWell(
            onTap: () async {
              Map<String, dynamic> addPlayData = {
                'vod_id': listDatas[index].vodId,
                'vod_pic': listDatas[index].vodPic,
                'vod_name': listDatas[index].vodName,
              };

              await SQliteHelp.instance.playVideoData(addPlayData);
              // print('当前点击${listDatas[index].vodId}');
              Navigator.of(context).push(
                MaterialPageRoute(builder: (_){
                  return VideoDetailPage( int.parse(listDatas[index].vodId) );
                })
              );
            },
            child: Container(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  Expanded(
                    child: Stack(
                      children: [
                        ExtendedImage.network(
                          listDatas[index].vodPic ?? 'https://via.placeholder.com/135x180?text=loading',
                          fit: BoxFit.cover,
                          width: double.infinity,
                          height: double.infinity,
                          shape: BoxShape.rectangle,
                          borderRadius: BorderRadius.all(Radius.circular(5)),
                          clearMemoryCacheWhenDispose: true,
                        ),
                        listDatas[index].vodRemarks.isNotEmpty || listDatas[index].vodLang.isNotEmpty ? Positioned(
                          top: 0,
                          right: 0,
                          child: Container(
                            padding: EdgeInsets.all(5),
                            decoration: BoxDecoration(
                              color: Colors.red,
                              borderRadius: BorderRadius.only(
                                bottomLeft: Radius.circular(5),
                                topRight: Radius.circular(5),
                              )
                            ),
                            child: Text(
                              '${listDatas[index].vodRemarks.isNotEmpty ? listDatas[index].vodRemarks.length < 8 ? listDatas[index].vodRemarks : listDatas[index].vodRemarks.substring(0,8) : listDatas[index].vodLang}',
                              overflow: TextOverflow.clip,
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: ScreenUtil.getInstance().getSp(9)
                              )
                            ),
                          )
                        ) : Container(),
                      ]
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.only(
                      top: 5
                    ),
                    alignment: Alignment.centerLeft,
                    child: Text(
                      '${listDatas[index].vodName}',
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: ScreenUtil.getInstance().getSp(12)
                      ),
                    )
                  ),
                ],
              ),
            ),
          );
        },
      ) : Align(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Icon(
              Icons.movie_filter,
              color: Colors.grey[300],
              size: 70,
            ),
            Text(
              '暂无数据',
              style: TextStyle(
                color: Colors.grey[600],
              ),
            )
          ],
        )
      ),
    );
  }


  // 获取随机数
  int getRandom() {
    int a = Random().nextInt(100);
    return a + 1000000000;
  }
  
  // 创建isolate
  Future<void> createIsolate() async {
    // 创建小红的接收器，用来接收小蓝的发送器
    ReceivePort redReceive = ReceivePort();
    // 创建 isolate， 并且把小红的发送器传给小蓝
    isolate = await Isolate.spawn<SendPort>(blueCounter, redReceive.sendPort);
    // 等待小蓝把发送器发送给小红
    blueSender = await redReceive.first;
    // 不用了记得关闭接收器
    redReceive.close();
  }

  // 利用compute计算
  computeCount() async {
    int random = getRandom();
    // compute 的回调函数必须是顶级函数或者static函数
    // int r = await compute(countEven, random);
  //  print('${random.toString()}有${r.toString()}个偶数');
  }

  // 开启isolate计算
  isolatePageData(List<VideoDetail>list, int pageIndex, bool status) async {
    // 创建一个临时传送装置
    ReceivePort _temp = ReceivePort();
    if(blueSender == null ){
      await createIsolate();
    }
    // 获取选择的视频类型
    ApplicationStatus applicationStatus = Provider.of<ApplicationStatus>(context, listen: false);
    // 用小蓝的发送装置发送一个消息包裹，里面是临时传送装置的发送器和要计算的数字
    blueSender.send(MessagePackage(_temp.sendPort, pageIndex, applicationStatus.selectVideoType, list, status, applicationStatus.currentIndex));
    // 等待临时传送装置返回计算结果
    List<VideoDetail> resultData = await _temp.first;
    _temp.close();
    if(!mounted) return;

    // 把计算结果告诉观众
    setState(() {
      if (resultData.isNotEmpty) {
        if ( status ) {
          listDatas = resultData;
          widget._refreshController.refreshCompleted();
        } else {
          listDatas.addAll(resultData);
          widget._refreshController.loadComplete();
        }
        // loadingPic();
      } else {
        listDatas = resultData;
        if ( status ) {
          widget._refreshController.refreshCompleted(resetFooterState: true);
        } else {
          widget._refreshController.loadNoData();
        }
      }
    });
  }

  // don't forget to dispose refreshController
  @override
  void dispose() {
    // widget._refreshController.dispose();
    isolate?.kill(priority: Isolate.immediate);
    super.dispose();
  }
}
